package com.example.springbott.wx_service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.kerbal.WxHomeIndexVo;
import com.example.springbott.bean.silic.WXHomeAboutVo;
import com.example.springbott.mapper.*;
import com.example.springbott.util.Count;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.example.springbott.util.Count.*;

@Component
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;


    @Override
    public WxHomeIndexVo wxHomeIndex() {
        //banner
        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo();
        List<MarketAd> marketAds = marketAdMapper.selectByExample(new MarketAdExample());
        wxHomeIndexVo.setBanner(marketAds);
        //brandList

        PageHelper.startPage(1, getMarketSystem(MALL_SYSTEM_WX_INDEX_BRAND));
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(new MarketBrandExample());
        wxHomeIndexVo.setBrandList(marketBrands);
        PageHelper.clearPage();
        //couponlist
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(new MarketCouponExample());
        wxHomeIndexVo.setCouponList(marketCoupons);
        //floorGoodsList
       PageHelper.startPage(1, getMarketSystem(MALL_SYSTEM_WX_INDEX_FLOOR));
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L2");
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketFloorList = marketCategoryMapper.selectByExample(marketCategoryExample);
       PageHelper.clearPage();
        List<WxHomeIndexVo.FloorGoodsListDTO> floorGoodsListDTOS = new ArrayList<WxHomeIndexVo.FloorGoodsListDTO>();
//        PageHelper.startPage(1, getMarketSystem("market_wx_catlog_goods"));
        for (int i = 0; i < marketFloorList.size(); i++) {
            WxHomeIndexVo.FloorGoodsListDTO floorGoodsListDTO = new WxHomeIndexVo.FloorGoodsListDTO();
            floorGoodsListDTO.setId(marketFloorList.get(i).getId());
            floorGoodsListDTO.setName(marketFloorList.get(i).getName());
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria categoryId = marketGoodsExample.createCriteria();
            categoryId.andDeletedEqualTo(false);
            categoryId.andCategoryIdEqualTo(marketFloorList.get(i).getId());
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            floorGoodsListDTO.setGoodsList(marketGoods);
            floorGoodsListDTOS.add(floorGoodsListDTO);
        }
//        PageHelper.clearPage();
        wxHomeIndexVo.setFloorGoodsList(floorGoodsListDTOS);
        //channel
        MarketCategoryExample marketCategoryExampleChan = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteriachannel = marketCategoryExampleChan.createCriteria();
        criteriachannel.andDeletedEqualTo(false);
        criteriachannel.andPidEqualTo(0);
        List<MarketCategory> channel= marketCategoryMapper.selectByExample(marketCategoryExampleChan);
        wxHomeIndexVo.setChannel(channel);
        //hotGoodsList
        PageHelper.startPage(1, getMarketSystem( MALL_SYSTEM_WX_INDEX_HOT ));
        MarketGoodsExample marketGoodsExampleHot = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteriaHot = marketGoodsExampleHot.createCriteria();
        criteriaHot.andIsHotEqualTo(true);
        criteriaHot.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExampleHot);
        wxHomeIndexVo.setHotGoodsList(marketGoods);
        PageHelper.clearPage();

        //newGoodsList
        PageHelper.startPage(1, getMarketSystem(MALL_SYSTEM_WX_INDEX_NEW));
        MarketGoodsExample marketGoodsExampleNew = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteriaNew = marketGoodsExampleNew.createCriteria();
        criteriaNew.andIsNewEqualTo(true);
        criteriaNew.andDeletedEqualTo(false);
        List<MarketGoods> marketGoodNew = marketGoodsMapper.selectByExample(marketGoodsExampleNew);
        wxHomeIndexVo.setNewGoodsList(marketGoodNew);
        PageHelper.clearPage();
        //topicList

        PageHelper.startPage(1, getMarketSystem(MALL_SYSTEM_WX_INDEX_TOPIC));
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria topicExampleCriteria = marketTopicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(new MarketTopicExample());
        wxHomeIndexVo.setTopicList(marketTopics);
        PageHelper.clearPage();
        return wxHomeIndexVo;

    }

    /**
     *  个人中心——关于我们about
     *
     * @param /null
     * @return {"errno":0,"data":{"qq":"12312312@qq.com","address":"test-demo1","phone":"19900092220","latitude":"31.201900","name":"商场配置test","longitude":"888"},"errmsg":"成功"}
     * @author  silic xia
     * @since 2022/11/23 17:16
     */
    @Override
    public WXHomeAboutVo wxHomeAbout() {

        String qq = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_QQ);
        String address = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_ADDRESS);
        String phone = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_PHONE);
        String latitude = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_LATITUDE);
        String name = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_NAME);
        String longtitude = marketSystemMapper.selectKeyValueByKeyName(Count.MALL_SYSTEM_LONGITUDE);

        WXHomeAboutVo data = new WXHomeAboutVo();
        data.setQq(qq);
        data.setAddress(address);
        data.setPhone(phone);
        data.setLatitude(latitude);
        data.setName(name);
        data.setLongitude(longtitude);
        return data;
    }


    private int getMarketSystem(String key) {
        MarketSystemExample marketSystemExampleBrand = new MarketSystemExample();
        MarketSystemExample.Criteria brandConfig = marketSystemExampleBrand.createCriteria();
        brandConfig.andKeyNameEqualTo(key);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExampleBrand);
        MarketSystem marketSystem = marketSystems.get(0);
        return Integer.parseInt(marketSystem.getKeyValue());
    }










}
